{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "09aa4408",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "# Transposed Convolution\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "64ac86cc",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T19:40:30.453582Z",
     "iopub.status.busy": "2023-08-18T19:40:30.453269Z",
     "iopub.status.idle": "2023-08-18T19:40:33.572095Z",
     "shell.execute_reply": "2023-08-18T19:40:33.570416Z"
    },
    "origin_pos": 2,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [],
   "source": [
    "import torch\n",
    "from torch import nn\n",
    "from d2l import torch as d2l"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7925e90a",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "Implement this basic transposed convolution operation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8d01fcf9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T19:40:33.580872Z",
     "iopub.status.busy": "2023-08-18T19:40:33.579987Z",
     "iopub.status.idle": "2023-08-18T19:40:33.594490Z",
     "shell.execute_reply": "2023-08-18T19:40:33.593361Z"
    },
    "origin_pos": 4,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [],
   "source": [
    "def trans_conv(X, K):\n",
    "    h, w = K.shape\n",
    "    Y = torch.zeros((X.shape[0] + h - 1, X.shape[1] + w - 1))\n",
    "    for i in range(X.shape[0]):\n",
    "        for j in range(X.shape[1]):\n",
    "            Y[i: i + h, j: j + w] += X[i, j] * K\n",
    "    return Y"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eb421189",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Validate the output of the above implementation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "75ed0b6d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T19:40:33.598934Z",
     "iopub.status.busy": "2023-08-18T19:40:33.598651Z",
     "iopub.status.idle": "2023-08-18T19:40:33.626958Z",
     "shell.execute_reply": "2023-08-18T19:40:33.625781Z"
    },
    "origin_pos": 6,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0.,  0.,  1.],\n",
       "        [ 0.,  4.,  6.],\n",
       "        [ 4., 12.,  9.]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = torch.tensor([[0.0, 1.0], [2.0, 3.0]])\n",
    "K = torch.tensor([[0.0, 1.0], [2.0, 3.0]])\n",
    "trans_conv(X, K)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "da946ae8",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Use high-level APIs to obtain the same results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e1ad37e5",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T19:40:33.632024Z",
     "iopub.status.busy": "2023-08-18T19:40:33.631545Z",
     "iopub.status.idle": "2023-08-18T19:40:33.647991Z",
     "shell.execute_reply": "2023-08-18T19:40:33.646736Z"
    },
    "origin_pos": 9,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[[ 0.,  0.,  1.],\n",
       "          [ 0.,  4.,  6.],\n",
       "          [ 4., 12.,  9.]]]], grad_fn=<ConvolutionBackward0>)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X, K = X.reshape(1, 1, 2, 2), K.reshape(1, 1, 2, 2)\n",
    "tconv = nn.ConvTranspose2d(1, 1, kernel_size=2, bias=False)\n",
    "tconv.weight.data = K\n",
    "tconv(X)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ce5644ef",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Padding, Strides, and Multiple Channels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1048beb2",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T19:40:33.653300Z",
     "iopub.status.busy": "2023-08-18T19:40:33.652500Z",
     "iopub.status.idle": "2023-08-18T19:40:33.662731Z",
     "shell.execute_reply": "2023-08-18T19:40:33.661823Z"
    },
    "origin_pos": 12,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[[4.]]]], grad_fn=<ConvolutionBackward0>)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tconv = nn.ConvTranspose2d(1, 1, kernel_size=2, padding=1, bias=False)\n",
    "tconv.weight.data = K\n",
    "tconv(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "72cccf5f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T19:40:33.667420Z",
     "iopub.status.busy": "2023-08-18T19:40:33.666693Z",
     "iopub.status.idle": "2023-08-18T19:40:33.676004Z",
     "shell.execute_reply": "2023-08-18T19:40:33.675089Z"
    },
    "origin_pos": 15,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[[0., 0., 0., 1.],\n",
       "          [0., 0., 2., 3.],\n",
       "          [0., 2., 0., 3.],\n",
       "          [4., 6., 6., 9.]]]], grad_fn=<ConvolutionBackward0>)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tconv = nn.ConvTranspose2d(1, 1, kernel_size=2, stride=2, bias=False)\n",
    "tconv.weight.data = K\n",
    "tconv(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "5f8aac99",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T19:40:33.679264Z",
     "iopub.status.busy": "2023-08-18T19:40:33.678924Z",
     "iopub.status.idle": "2023-08-18T19:40:33.724131Z",
     "shell.execute_reply": "2023-08-18T19:40:33.723022Z"
    },
    "origin_pos": 18,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = torch.rand(size=(1, 10, 16, 16))\n",
    "conv = nn.Conv2d(10, 20, kernel_size=5, padding=2, stride=3)\n",
    "tconv = nn.ConvTranspose2d(20, 10, kernel_size=5, padding=2, stride=3)\n",
    "tconv(conv(X)).shape == X.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2c8b4262",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "Connection to Matrix Transposition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "54c1abd6",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T19:40:33.727935Z",
     "iopub.status.busy": "2023-08-18T19:40:33.727252Z",
     "iopub.status.idle": "2023-08-18T19:40:33.735227Z",
     "shell.execute_reply": "2023-08-18T19:40:33.734426Z"
    },
    "origin_pos": 20,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[27., 37.],\n",
       "        [57., 67.]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = torch.arange(9.0).reshape(3, 3)\n",
    "K = torch.tensor([[1.0, 2.0], [3.0, 4.0]])\n",
    "Y = d2l.corr2d(X, K)\n",
    "Y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "5c83e2aa",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T19:40:33.738704Z",
     "iopub.status.busy": "2023-08-18T19:40:33.738150Z",
     "iopub.status.idle": "2023-08-18T19:40:33.746690Z",
     "shell.execute_reply": "2023-08-18T19:40:33.745684Z"
    },
    "origin_pos": 22,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1., 2., 0., 3., 4., 0., 0., 0., 0.],\n",
       "        [0., 1., 2., 0., 3., 4., 0., 0., 0.],\n",
       "        [0., 0., 0., 1., 2., 0., 3., 4., 0.],\n",
       "        [0., 0., 0., 0., 1., 2., 0., 3., 4.]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def kernel2matrix(K):\n",
    "    k, W = torch.zeros(5), torch.zeros((4, 9))\n",
    "    k[:2], k[3:5] = K[0, :], K[1, :]\n",
    "    W[0, :5], W[1, 1:6], W[2, 3:8], W[3, 4:] = k, k, k, k\n",
    "    return W\n",
    "\n",
    "W = kernel2matrix(K)\n",
    "W"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "444dbc7d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T19:40:33.750344Z",
     "iopub.status.busy": "2023-08-18T19:40:33.749752Z",
     "iopub.status.idle": "2023-08-18T19:40:33.757265Z",
     "shell.execute_reply": "2023-08-18T19:40:33.756389Z"
    },
    "origin_pos": 24,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[True, True],\n",
       "        [True, True]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Y == torch.matmul(W, X.reshape(-1)).reshape(2, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "e1834374",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T19:40:33.761069Z",
     "iopub.status.busy": "2023-08-18T19:40:33.760455Z",
     "iopub.status.idle": "2023-08-18T19:40:33.767618Z",
     "shell.execute_reply": "2023-08-18T19:40:33.766760Z"
    },
    "origin_pos": 26,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[True, True, True],\n",
       "        [True, True, True],\n",
       "        [True, True, True]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Z = trans_conv(Y, K)\n",
    "Z == torch.matmul(W.T, Y.reshape(-1)).reshape(3, 3)"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "language_info": {
   "name": "python"
  },
  "required_libs": [],
  "rise": {
   "autolaunch": true,
   "enable_chalkboard": true,
   "overlay": "<div class='my-top-right'><img height=80px src='http://d2l.ai/_static/logo-with-text.png'/></div><div class='my-top-left'></div>",
   "scroll": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}